package com.zjw.zy.coreui.fragment.access

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.zjw.zy.R
import com.zjw.zy.base.BaseFragment
import com.zjw.zy.utils.AppUtils
import com.zjw.zy.utils.OcrUtils
import com.zjw.zy.utils.WindowUtils

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2022/10/20 9:14
 * @description：授权页面
 */
class AccessFragment : BaseFragment() {

    companion object {
        const val SUB_INFO_INDEX = 0
        const val SUB_ID_INDEX = 1
        const val SUB_BIND_INDEX = 2
        const val SUB_RESULT_INDEX = 3
    }

    private var isAccess = AppUtils.isAccess()

    private val content: ViewPager2 by lazy { findViewById(R.id.access_fragment_content) }

    override fun onPageCreate() {
        super.onPageCreate()
        if (!OcrUtils.isAccess()) {
            OcrUtils.init(AppUtils.getApp())
        }
    }


    override fun onPageEnter() {

        WindowUtils.fullMode()

    }

    override fun onViewRestore(savedInstanceState: Bundle?) {

    }

    override fun onSubscribe() {

    }

    override fun onBindViewListener() {

    }

    override fun onBindViewValue() {
        content.adapter = AccessSubFragmentAdapter(this)
        content.isUserInputEnabled = false
        content.isSaveEnabled = false
    }

    override fun onFindView(rootView: View?) {

    }

    override fun onLayoutId(): Int {
        return R.layout.fragment_access
    }

    override fun onPageExit() {

    }

    fun switchPage(index: Int) {
        content.setCurrentItem(index, true)
    }


    private inner class AccessSubFragmentAdapter(parent: Fragment) : FragmentStateAdapter(parent) {

        override fun getItemCount(): Int {
            return if (isAccess) {
                1
            } else {
                4
            }
        }

        override fun createFragment(position: Int): Fragment {
            if (isAccess) {
                return AccessResultFragment()
            }
            when (position) {
                SUB_INFO_INDEX -> {
                    return AccessInfoFragment()
                }
                SUB_ID_INDEX -> {
                    return AccessIdFragment()
                }

                SUB_BIND_INDEX -> {
                    return AccessBindFragment()
                }

                SUB_RESULT_INDEX -> {
                    return AccessResultFragment()
                }
            }
            return AccessInfoFragment()
        }

    }

    override fun onBackClick() {
        if (AppUtils.isAccess()) {
            super.onBackClick()
        } else {
            super.onCallMoveToBack()
        }
    }


}